use tauri::{
    image::Image,
    menu::{Menu, MenuItemBuilder},
    tray::{TrayIcon, TrayIconBuilder, TrayIconEvent, MouseButton, MouseButtonState},
    App, Manager,
};

// 创建系统托盘菜单
pub fn create_tray_menu(app: &mut App) -> Result<TrayIcon, tauri::Error> {
    // 创建菜单项
    let show = MenuItemBuilder::with_id("show", "显示窗口").build(app)?;
    let hide = MenuItemBuilder::with_id("hide", "隐藏窗口").build(app)?;
    let quit = MenuItemBuilder::with_id("quit", "退出").build(app)?;
    let about = MenuItemBuilder::with_id("about", "关于").build(app)?;
    // 创建菜单
    let tray_menu = Menu::with_items(app, &[&show, &hide, &quit,&about])?;
    // 创建菜单
    // 获取应用句柄
    let app_handle = app.handle();

    // 创建系统托盘，使用图标文件
    let icon: tauri::image::Image = Image::from_bytes(include_bytes!("../icons/icon.ico"))?;
    let my_tray_icon = 
    TrayIconBuilder::new()
        .icon(icon)
        .title("我的应用")
        .tooltip("我的应用")
        .show_menu_on_left_click(false)
        .menu(&tray_menu)
        .on_menu_event({
            let app_handle = app_handle.clone();
            move |_tray, event| {
                // 获取应用句柄
                match event.id().as_ref() {
                    "quit" => {
                        println!("系统托盘：退出应用");
                        app_handle.exit(0);
                    }
                    "show" => {
                        println!("系统托盘：显示窗口");
                        if let Some(window) = app_handle.get_webview_window("main") {
                            window.show().unwrap();
                            window.set_focus().unwrap();
                        }
                    }
                    "hide" => {
                        println!("系统托盘：隐藏窗口");
                        if let Some(window) = app_handle.get_webview_window("main") {
                            window.hide().unwrap();
                        }
                    }
                    _ => {}
                }
            }
        })
        .on_tray_icon_event({
            let app_handle = app_handle.clone();
            move |_tray_id, event| {
                match event {
                    TrayIconEvent::Click { button, button_state, .. } => {
                        // 左键单击（按下）时切换窗口显示状态
                        if (button == MouseButton::Left) && (button_state == MouseButtonState::Up) {
                            // 获取窗口并切换显示状态
                            println!("左键点击托盘图标（按下），切换窗口");
                            if let Some(window) = app_handle.get_webview_window("main") {
                                match window.is_visible() {
                                    Ok(true) => {
                                        let _ = window.hide();
                                    }
                                    Ok(false) => {
                                        let _ = window.show();
                                        let _ = window.set_focus();
                                    }
                                    Err(e) => println!("获取窗口状态失败: {}", e),
                                }
                            }
                        }
                    },
                    _ => {}
                }
            }
        })
        .build(app);
    my_tray_icon

}
